var assert = require('assert');
var logger = using('easynode.framework.Logger').forFile(__filename);
var GenericObject = using('easynode.GenericObject');
var ActionResult = using('easynode.framework.mvc.ActionResult');
var utility = require('utility');
var HTTPUtil = using('easynode.framework.util.HTTPUtil');
var BeanFactory = using('easynode.framework.BeanFactory');
var thunkify = require('thunkify');
var md5 = require('md5');


(function () {
        /**
         * Class UserController
         *
         * @class #NAMESPACE#.UserController
         * @extends easynode.GenericObject
         * @since 0.1.0
         * @author duansj
         * */
        class UserController extends GenericObject {
                /**
                 * 构造函数。
                 *
                 * @method 构造函数
                 * @since 0.1.0
                 * @author zlbbq
                 * */
                constructor() {
                        super();
                        //调用super()后再定义子类成员。
                        this.bookshelf = null;                          //IoC injection
                        this.cache = null;
                        this.util=null;
                        this.requestWarpper=null;
                }

                initialize()
                {
                        this.request=this.requestWarpper.getBaseRequest();
                }


                /**
                 * @api {post} /api/user/login 用户登录
                 * @apiName login
                 * @apiGroup user
                 *
                 * @apiParam {String} mobile 用户手机号 *
                 * @apiParam {String} password　用户明文密码的MD5值 *
                 *
                 * @apiSuccess {Number} code 返回码，0 -> 成功
                 * @apiSuccess {Object} result API处理结果
                 * @apiSuccess {String} msg 对应于返回码的文本消息
                 * */
                login() {
                        var me = this;
                        return function * () {
                                this.validator.check('mobile').necessary().match(me.util.getRegExps().mobile).end();
                                this.validator.check('password').necessary().min(0).end();

                                //for test
                                var params=
                                {
                                        mobile:this.p('mobile'),
                                        password:this.p('password'),
                                        device:'WX'
                                }

                                //logger.error('simlate pwd:'+md5(this.p('password')));

                                if (this.validator.isValid()) {
                                        var j = me.request.jar();
                                        var response = yield me.request({
                                                jar:j,
                                                uri: "/v1/login",
                                                form: params
                                        });

                                        var requestUrl=response.request.uri.href;
                                        var cookie_string = j.getCookieString(requestUrl); // 获取服务端cookie
                                        yield me.util.setCookie(this.p('mobile'),cookie_string);
                                      //  logger.error("response:"+JSON.stringify(response.toJSON()));

                                        return me.util.handerResult(response);
                                }
                        };
                }



                /**
                 * @api {post} /api/user/getIdentifyingCode 获取手机验证码
                 * @apiName getIdentifyingCode
                 * @apiGroup user
                 *
                 * @apiParam {String} mobile 用户手机号 *
                 * @apiParam {String} type　申请验证码类型 0为注册， 1为密码重置*
                 *
                 * @apiSuccess {Number} code 返回码，0 -> 成功
                 * @apiSuccess {Object} result API处理结果
                 * @apiSuccess {String} msg 对应于返回码的文本消息
                 * */
                getIdentifyingCode()
                {
                        var me=this;
                        return function *()
                        {
                                this.validator.check('mobile').necessary().match(me.util.getRegExps().mobile).end();
                                this.validator.check('type').necessary().enum(['0','1']).end();
                                if (this.validator.isValid()) {
                                        var response = yield me.request({
                                                uri: "/v1/getIdentifyingCode",
                                                form:
                                                {
                                                        "mobile":this.p("mobile"),
                                                        "type":this.p("type")
                                                }
                                        });
                                        return me.util.handerResult(response);
                                }
                        }
                }



                /**
                 * @api {post} /api/user/register 用户注册
                 * @apiName register
                 * @apiGroup user
                 *
                 * @apiParam {String} mobile  用户手机号 *
                 * @apiParam {String} password　用户明文密码的MD5值 *
                 * @apiParam {String} identifycode　用户手机验证码 *
                 *
                 * @apiSuccess {Number} code 返回码，0 -> 成功
                 * @apiSuccess {Object} result API处理结果
                 * @apiSuccess {String} msg 对应于返回码的文本消息
                 * */
                register()
                {
                        var me=this;
                        return function *()
                        {
                                this.validator.check('mobile').necessary().match(me.util.getRegExps().mobile).end();
                                this.validator.check('password').necessary().end();
                                this.validator.check('identifycode').necessary().end();
                                if (this.validator.isValid()) {
                                        var response = yield me.request({
                                                uri: "/v1/register",
                                                form: {
                                                        "mobile": this.p("mobile"),
                                                        "password": this.p("password"),
                                                        "identifycode": this.p('identifycode'),
                                                        "device":'wx'  //服务器做了必传验证
                                                }
                                        });
                                        return me.util.handerResult(response);
                                }
                        }

                }


                /**
                 * @api {post} /api/user/resetPasswd 密码重置接口
                 * @apiName resetPasswd
                 * @apiGroup user
                 *
                 * @apiParam {String} mobile  用户手机号 *
                 * @apiParam {String} password　用户明文密码的MD5值 *
                 * @apiParam {String} identifycode　用户手机验证码 *
                 *
                 * @apiSuccess {Number} code 返回码，0 -> 成功
                 * @apiSuccess {Object} result API处理结果
                 * @apiSuccess {String} msg 对应于返回码的文本消息
                 * */
                resetPasswd()
                {
                        var me=this;
                        return function *()
                        {
                                this.validator.check('mobile').necessary().match(me.util.getRegExps().mobile).end();
                                this.validator.check('password').necessary().end();
                                this.validator.check('identifycode').necessary().end();
                                if (this.validator.isValid()) {
                                        var response = yield me.request({
                                                uri: "/v1/resetPasswd",
                                                form: {
                                                        "mobile": this.p("mobile"),
                                                        "password": this.p("password"),
                                                        "identifycode": this.p('identifycode')
                                                }
                                        });
                                        return me.util.handerResult(response);
                                }
                        }
                }


                /**
                 * @api {post} /api/user/modifyPasswd 密码修改接口
                 * @apiName modifyPasswd
                 * @apiGroup user
                 *
                 * @apiParam {String} mobile  用户手机号 *
                 * @apiParam {String} oldpassword　用户旧密码的MD5值  *
                 * @apiParam {String} newpassword　用户新密码 *
                 *
                 * @apiSuccess {Number} code 返回码，0 -> 成功
                 * @apiSuccess {Object} result API处理结果
                 * @apiSuccess {String} msg 对应于返回码的文本消息
                 * */
                modifyPasswd()
                {
                        var me=this;
                        return function *()
                        {
                                this.validator.check('mobile').necessary().match(me.util.getRegExps().mobile).end();
                                this.validator.check('oldpassword').necessary().end();
                                this.validator.check('newpassword').necessary().end();
                                if (this.validator.isValid()) {
                                        var response = yield me.request({
                                                uri: "/v1/modifyPasswd",
                                                form: {
                                                        "mobile": this.p("mobile"),
                                                        "oldpassword": this.p("oldpassword"),
                                                        "newpassword": this.p('newpassword')
                                                }
                                        });
                                        return me.util.handerResult(response);
                                }
                        }


                }


                getClassName() {
                        return EasyNode.namespace(__filename);
                }
        }

        module.exports = UserController;
})();